﻿using RPAVIEW.util;
using RPAVIEW.views.controlLibrary;
using RPAVIEW.views.OtherPages;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.util;
using System.Windows;
using System.Windows.Controls;
using FScreen = System.Windows.Forms.Screen;

namespace RPAVIEW.views.EditPage.flows.DownLoadFileN {
    /// <summary>
    /// Input.xaml 的交互逻辑
    /// </summary>
    public partial class Input:BaseParamViewAction{
        public Input() {
            InitializeComponent();
            DownLoadFileNParam param = ParamUtil.GetCurrentParam<DownLoadFileNParam>();
            if(param.RenameDownloadFile) {
                RameFileChecked(null, null);
            } else { 
                RenameFileUnchecked(null, null);
            }

            if(param.WaitingDownloadFinish) {
                WaitDownloadChecked(null, null);
            } else { 
                WaitDownloadUnchecked(null, null);
            }
        }

        public override void UpdateSource() {
            this.webObj.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.downloadMethod.GetBindingExpression(ComboBox.SelectedIndexProperty).UpdateSource();
            this.eleImg.GetBindingExpression(Image.SourceProperty).UpdateSource();
            this.downloadAddr.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.savePath.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.renameDownloadFile.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            this.saveFileName.GetBindingExpression(SelVarTextBox.text).UpdateSource();
            this.waitingDownloadFinish.GetBindingExpression(CheckBox.IsCheckedProperty).UpdateSource();
            this.expireTime.GetBindingExpression(SelVarTextBox.text).UpdateSource();
        }

        private void SelectSavePath(object sender, RoutedEventArgs e) {
            System.Windows.Forms.FolderBrowserDialog saveDialog =  FileUtil.OpenFloderSelectDialog();
            System.Windows.Forms.DialogResult dialogResult = saveDialog.ShowDialog();
            //按下确定选择的按钮  
            if(dialogResult == System.Windows.Forms.DialogResult.OK) {
                //记录选中的目录
                this.savePath.Text = saveDialog.SelectedPath;
            }
        }

        /// <summary>
        /// 由于目前并没有元素库功能，所以现在所有元素操作都必须直接选择。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetElement(object sender, RoutedEventArgs e) {
            MainWindow main = WindowUtil.GetWindow<MainWindow>();
            WindowUtil.MinSizeWindow(main);
            GetEleWindow eleWindow = new GetEleWindow((ele)=>{
                DownLoadFileNParam param = ParamUtil.GetCurrentParam<DownLoadFileNParam>();
                param.WebEle = ele;
            });
            eleWindow.Show();
            eleWindow.RegistHotKey();
        }

        private void downloadMethodChanged(object sender, SelectionChangedEventArgs e) {
            int method = this.downloadMethod.SelectedIndex;
            if(method == 0) {
                this.eleRow.Height = GridLength.Auto;
                this.addrRow.Height = new GridLength(0);
            } else { 
                this.eleRow.Height = new GridLength(0);
                this.addrRow.Height = GridLength.Auto;
            }
        }

        private void RameFileChecked(object sender, RoutedEventArgs e) {
            this.fileNameRow.Height = GridLength.Auto;
        }

        private void RenameFileUnchecked(object sender, RoutedEventArgs e) {
             this.fileNameRow.Height = new GridLength(0);
        }

        private void WaitDownloadUnchecked(object sender, RoutedEventArgs e) {
            this.expireTimeRow.Height = new GridLength(0);
        }

        private void WaitDownloadChecked(object sender, RoutedEventArgs e) {
            this.expireTimeRow.Height = GridLength.Auto;
        }
    }
}
